package com.mach.platform.controller;

import com.mach.platform.domain.DevFollow;
import com.mach.platform.domain.Response;
import com.mach.platform.domain.iot.IotDev;
import com.mach.platform.service.DevFollowServiceImpl;
import com.mach.platform.utils.security.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author Administrator
 * @Description:
 * @Package: com.mach.platform.controller
 * @time 2018/7/20 0020 11:03
 */
@Api(tags = { "关注设备 接口" })
@RestController
@RequestMapping("$follow")
public class DevFollowCtrl {

	@Autowired
	private DevFollowServiceImpl devFollowService;

	/**
	 * 保存新的设备关注
	 *
	 * @param iotDev
	 * @return
	 */
	@ApiOperation(value = "保存新的关注", notes = "null", produces = "application/json")
	@PostMapping("follow")
	public Response save (@ApiParam(name = "iotDev", value = "必须包含设备id的对象{id:xx},json发送") @RequestBody IotDev iotDev) {

		DevFollow newObj = devFollowService.save(DevFollow.builder().device(iotDev)
		                                                  .user(SecurityUtils.getCurrentUserByAccountDomain()).build());

		if (newObj != null) {
			return Response.ok(newObj);
		} else {
			return Response.error("已经关注了该设备，不能重复操作！");
		}

	}

	/**
	 * 取消设备关注
	 *
	 * @param iotDev
	 * @return
	 */
	@ApiOperation(value = "取消设备关注", notes = "null", produces = "application/json")
	@PostMapping("cancel")
	public Response delete (
			@ApiParam(name = "iotDev", value = "必须包含设备id的对象{id:xx},json发送") @RequestBody IotDev iotDev) {

		int effections = devFollowService.deleteUserFollowData(iotDev);

		return Response.ok(effections);
	}

	/**
	 * 获取当前用户的关注 设备信息
	 *
	 * @return
	 */
	@ApiOperation(value = "获取当前用户的关注 设备信息", notes = "null", produces = "application/json")
	@PostMapping("list")
	public Response list ( ) {

		List<IotDev> follows = devFollowService.loadUserDevsFollowing();
		return Response.ok(follows);
	}

}
